# Disable in-source builds to prevent source tree corruption.
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
         message(FATAL_ERROR "FATAL: In-source builds are not allowed.
        You should create a separate directory for build files.")
endif()

cmake_minimum_required(VERSION 3.0.0)

if(POLICY CMP0048)
  cmake_policy(SET CMP0048 NEW)
endif()

PROJECT(coap-demo)

SET(CMAKE_BUILD_TYPE Release)

SET(PRO_TOP_PATH "${PROJECT_SOURCE_DIR}")

# 指定 C 和 C++ 编译器
# if (AARCH64_HOST)
#   set(CMAKE_C_COMPILER arm-linux-musleabi-gcc)
#   set(CMAKE_CXX_COMPILER arm-linux-musleabi-g++)
#   set(CMAKE_INSTALL_LIBDIR "/data/coap-demo/libs")
#   set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
# endif()



# 设置链接时使用的库路径（这些路径将被添加到 RPATH 中，如果 INSTALL_RPATH_USE_LINK_PATH 被设置为 TRUE）
#link_directories(/path/to/first/lib /path/to/second/lib)
# 注意：link_directories 在新版本的 CMake 中不推荐使用，因为它不便于管理路径依赖。
# 更好的做法是使用 target_link_libraries 直接指定库的完整路径或名称（如果库在标准路径下）。


# Overall build libraries
if (MQTT_CLIENT_EXTRA)

  add_library(mqttclient SHARED
              mqtt_client_publish.c
              mqtt_client_subscribe.c)

  target_include_directories(mqttclient PUBLIC ${PRO_TOP_PATH}/include)

  if (ARM_HOST)
    set(CMAKE_C_COMPILER arm-linux-musleabi-gcc)
    set(CMAKE_CXX_COMPILER arm-linux-musleabi-g++)
    target_link_libraries(mqttclient
      /opt/arm-linux-musleabi/cJSON/usr/local/lib/libcjson.so.1
      ${PRO_TOP_PATH}/lib/aarch64/libmosquitto.so.1)
    
    add_executable(mqtt-client mqtt_client.c)
    target_include_directories(mqtt-client PUBLIC ${PRO_TOP_PATH}/include)
    target_link_libraries(mqtt-client
    /opt/arm-linux-musleabi/cJSON/usr/local/lib/libcjson.so.1
      /opt/arm-linux-musleabi/openssl/lib64/libssl.so.3
      /opt/arm-linux-musleabi/openssl/lib64/libcrypto.so.3
      ${PRO_TOP_PATH}/lib/arm/libmosquitto.so.1
      ${PRO_TOP_PATH}/lib/arm/libmqttclient.so)
    
    add_executable(mqtt-client-pub mqtt_client_pub.c)
    target_include_directories(mqtt-client-pub PUBLIC ${PRO_TOP_PATH}/include)
    target_link_libraries(mqtt-client-pub
      /opt/arm-linux-musleabi/cJSON/usr/local/lib/libcjson.so.1
      /opt/arm-linux-musleabi/openssl/lib64/libssl.so.3
      /opt/arm-linux-musleabi/openssl/lib64/libcrypto.so.3
      ${PRO_TOP_PATH}/lib/arm/libmosquitto.so.1
      ${PRO_TOP_PATH}/lib/arm/libmqttclient.so)
    
  endif()

  # build aarch64 
  if (AARCH64_HOST)
    set(CMAKE_C_COMPILER aarch64-linux-musl-gcc)
    set(CMAKE_CXX_COMPILER aarch6libmosquitto.so.14-linux-musl-g++)
    target_link_libraries(mqttclient
      /opt/aarch64-linux-musleabi/cJSON/lib/libcjson.so.1
      ${PRO_TOP_PATH}/lib/aarch64/libmosquitto.so.1)
    
    add_executable(mqtt-client mqtt_client.c)
    target_include_directories(mqtt-client PUBLIC ${PRO_TOP_PATH}/include)
    target_link_libraries(mqtt-client
      /opt/aarch64-linux-musleabi/cJSON/lib/libcjson.so.1
      /opt/aarch64-linux-musleabi/openssl-3.2.3/lib64/libssl.so.3
      /opt/aarch64-linux-musleabi/openssl-3.2.3/lib64/libcrypto.so.3
      ${PRO_TOP_PATH}/lib/aarch64/libmosquitto.so.1
      ${PRO_TOP_PATH}/lib/aarch64/libmqttclient.so)
    
    add_executable(mqtt-client-pub mqtt_client_pub.c)
    target_include_directories(mqtt-client-pub PUBLIC ${PRO_TOP_PATH}/include)
    target_link_libraries(mqtt-client-pub
      /opt/aarch64-linux-musleabi/cJSON/lib/libcjson.so.1
      /opt/aarch64-linux-musleabi/openssl-3.2.3/lib64/libssl.so.3
      /opt/aarch64-linux-musleabi/openssl-3.2.3/lib64/libcrypto.so.3
      ${PRO_TOP_PATH}/lib/aarch64/libmosquitto.so.1
      ${PRO_TOP_PATH}/lib/aarch64/libmqttclient.so)
  endif()

  if (X86_64_HOST)
  target_link_libraries(mqttclient
      ${PRO_TOP_PATH}/linux/libmosquitto.so.2.0.20)
  endif()

endif()